
import 'dart:io';

import 'package:elmer_tools/common/dataBase/DataBaseAdapter.dart';
import 'package:elmer_tools/common/dataBase/DataTable.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

class SqliteAdapter implements DataBaseAdapter {
    List<DataTable> tables = [];
    SqliteAdapter(this.tables);

    @override
    Future<void> clear() {
        throw UnimplementedError();
    }

    @override
    Future<T> get<T>(String key) {
        throw UnimplementedError();
    }

    @override
    Future<void> remove(String key) {
        throw UnimplementedError();
    }

    @override
    Future<void> set<T>(String key, T value) {
        throw UnimplementedError();
    }


    @override
    void init() {
        if (!Platform.isAndroid && !Platform.isIOS) {
            // 初始化 FFI
            sqfliteFfiInit();
            
            // 设置数据库工厂
            databaseFactory = databaseFactoryFfi;
        }
        for(DataTable table in tables) {
            String tableSql = "CREATE TABLE IF NOT EXISTS ${table.tableName} (\n";
            List<String> uniqueColumn = [];
            table.columns.forEach((column) {
                // Here you would typically create the table in the SQLite database
                // using the column definitions.
                tableSql += "    ${column.type},\n";
                if(column.unique == true) {
                    uniqueColumn.add(column.name);
                }
            });
            if(uniqueColumn.isNotEmpty) {
                tableSql += uniqueColumn.map((name) => "    UNIQUE($name)").join(",\n");// "UNIQUE (${uniqueColumn.join(", ")})\n";
            }
            tableSql += "\n);";
            print("-- SQL Table Creation --\n$tableSql\n--");
        }
        _initDataBase();
    }
    void _initDataBase() async {
        final databasePath = await getDatabasesPath();
        final path = join(databasePath, 'app_database.db');
        print("Database path: $path");
    }
        
}